package com.yc.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.yc.biz.OrderBiz;
import com.yc.dao.DBHelper;

public class OrderBizImpl implements OrderBiz {

	DBHelper db=new DBHelper();
	
	@Override//添加订单//订单表需要订单号null,支付状态 ptid:付款方式,uid:用户号,aid:地址的id，gid:商品号 ,date:日期,allprice:此次订单的金额
	public int addOrder(int uid, int ptid, int aid, String str,String strNum, String date,String strMoney) {
		//首先先将此用户选中的商品从购物表中删除  然后再将信息添加进订单表
		//获取gid  gid存在str中  str="13,5"
		String ss="";
		int length=str.split(",").length;
		System.out.println("数组的长度为"+length);
		for(int i=0;i<length-1;i++){
			ss+=str.split(",")[i]+",";
		}
		ss+=str.split(",")[length-1];
		System.out.println("ss="+ss);
				
		int result=0;
		int result1=0;
		System.out.println("result1="+result1);
				
				
		//删除成功再讲商品添加至order表中
		//insert into order values(null,1,2,1,17,'2019-10-12',3333),
		String sql1="insert into orderinfo values ";
		int gid=0;
		double money=0;
		int num=0;
		for(int i=0;i<length-1;i++){
			gid=Integer.parseInt(str.split(",")[i]);
			money=Double.parseDouble(strMoney.split(",")[i]);
			num=Integer.parseInt(strNum.split(",")[i]);
			System.out.println(gid+"----"+money+"---"+num);
			sql1+="(null,"+ptid+","+uid+","+aid+","+gid+","+num+",'"+date+"',"+money+",0),";
		}
		gid=Integer.parseInt(str.split(",")[length-1]);
		money=Double.parseDouble(strMoney.split(",")[length-1]);
		num=Integer.parseInt(strNum.split(",")[length-1]);
		sql1+="(null,"+ptid+","+uid+","+aid+","+gid+","+num+",'"+date+"',"+money+",0)";
		result1=db.doUpdate(sql1, null);
				
		if(result1>0){
			//添加成功在将商品从购物车中删除
			String sql="delete from cartinfo where uid="+uid+" and gid in("+ss+")";
			result=db.doUpdate(sql, null);
		}		
		return result;
	}

	@Override
	//查询所有订单   日期 订单号 支付状态 商品名 单价 数量 总价
	public List<Map<String, String>> findAllOrder(int uid,int page) {
		//分页查询
		//假设一页3条数据
		int count=3;
		//得到总共有多少条数据
		int size=findGoodsByTno(uid);
		//得到这些数据要分多少页
		int pages=size%count==0?size/count:size/count+1;
		
		String sql="select orderinfo.*,price,tname,pic,ptype from orderinfo,good,paytype where orderinfo.gid=good.gid and orderinfo.ptid=paytype.ptid and orderinfo.uid="+uid+" limit "+(page-1)*count+","+count;
		List<Map<String, String>> list=db.findAll(sql, null);
		//将当前页和最大页，也一起存到list里面去
		Map<String, String> maps=new HashMap<String, String>();
		maps.put("page", page+"");
		maps.put("pages", pages+"");
		//把map添加到list里面去
		list.add(maps);
		
		return list;
	}

	@Override
	//查找总条数
	public int findGoodsByTno(int uid) {
		String sql="select * from orderinfo where uid="+uid;
		return db.findAll(sql, null).size();
	}

	@Override
	//判断支付密码是否正确
	public int payOrder(int uid, String paypwd) {
		String sql="select * from user where uid=? and paypwd=?";
		List<Object> params=new ArrayList<Object>();
		params.add(uid);
		params.add(paypwd);
		return db.findAll(sql, params).size();
	}

	@Override//改变订单的状态
	public int updateStatus(int uid, String oid, double price) {
		int length=oid.split(",").length;
		//System.out.println(length);   12,13,
		String ss="";
		for(int i=0;i<length-1;i++){
			ss+="?,";
		}
		ss+="?";
		String sql="update orderinfo set status=1 where oid in("+ss+")";
		List<Object> params=new ArrayList<Object>();
		for(int i=0;i<length;i++){
			int id=Integer.parseInt(oid.split(",")[i]);
			//System.out.println(id);
			params.add(id);
		}
		//对订单状态进行改变
		int result=db.doUpdate(sql, params);
		if(result>0){
			//修改用户的余额
			//首先从订单表中查找到这个商品的支付方式然后
			String sql1="select ptid from orderinfo where oid in("+ss+")";
			int ptid=Integer.parseInt( db.findAll(sql1, params).get(0).get("ptid") );
			
			//根据支付方式来修改金额
			double money=findMoneyByPtid(uid,oid);
			money=money-price;
			String sql2="update payuseinfo set money="+money+" where uid="+uid+" and ptid="+ptid;
			int flag=db.doUpdate(sql2, null);
			if(flag>0){
				return 1;
			}else{
				return 0;
			}
		}else{
			return 0;
		}
	}

	@Override
	public double findMoneyByPtid(int uid,String oid) {
		
		int length=oid.split(",").length;
		//System.out.println(length);   12,13,
		String ss="";
		for(int i=0;i<length-1;i++){
			ss+="?,";
		}
		ss+="?";
		List<Object> params=new ArrayList<Object>();
		for(int i=0;i<length;i++){
			int id=Integer.parseInt(oid.split(",")[i]);
			//System.out.println(id);
			params.add(id);
		}
		String sql1="select ptid from orderinfo where oid in("+ss+")";
		int ptid=Integer.parseInt( db.findAll(sql1, params).get(0).get("ptid") );
		
		//根据支付方式来修改金额
		String sql="select money from payuseinfo where uid="+uid+" and ptid="+ptid;
		double money=Double.parseDouble(db.findAll(sql, null).get(0).get("money"));
		return money;
	}
	
	@Override//判断支付密码是否为空
	public String findPayPwd(int uid) {
		String sql="select * from user where uid="+uid;
		String pwd=db.findAll(sql,null).get(0).get("paypwd");
		//System.out.println(pwd);
		return pwd;
	}
	
}
